package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"strconv"
	"time"
)

var (
	host  *string = flag.String("host", "127.0.0.1:8081", "host")
	begin *string = flag.String("begin", "2006-01-02 15:04:05", "begin time")
	end   *string = flag.String("end", "2006-01-02 15:04:05", "end time")
)

func main() {
	flag.Parse()
	if *host == "" || *begin == "" || *end == "" {
		fmt.Println("host, begin, end is necessary")
		return
	}
	fmt.Println("host", *host, "begin", *begin, "end", *end)
	stru := fmt.Sprintf("http://%s/todo", *host)
	ub, err := time.ParseInLocation("2006-01-02 15:04:05", *begin, time.Local)
	if err != nil {
		fmt.Println("time format mast be like YYYY-MM-DD hh:mm:ss")
		return
	}
	ue, err := time.ParseInLocation("2006-01-02 15:04:05", *end, time.Local)
	if err != nil {
		fmt.Println("time format mast be like YYYY-MM-DD hh:mm:ss")
		return
	}
	strb := ub.Unix()
	stre := ue.Unix()

	b := strconv.FormatInt(strb, 10)
	e := strconv.FormatInt(stre, 10)

	params := url.Values{}
	Url, _ := url.Parse(stru)
	params.Set("command", "HistoryRecall")
	params.Set("begin", b)
	params.Set("end", e)
	fmt.Println(time.Unix(int64(strb), 0), time.Unix(int64(stre), 0))
	//如果参数中有中文参数,这个方法会进行URLEncode
	Url.RawQuery = params.Encode()
	urlPath := Url.String()
	fmt.Println(urlPath) //等同于https://www.xxx.com?begin=23&end=456
	resp, err := http.Get(urlPath)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)
	fmt.Println(string(body))
}
